package com.lengmou.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lengmou.entity.Goods;
import com.lengmou.entity.GoodsComment;
import com.lengmou.service.GoodsCommentService;
import com.lengmou.service.GoodsService;
import com.lengmou.service.GoodsTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsTypeService goodsTypeService;
    @Resource
    private GoodsCommentService goodsCommentService;

//    搜索jsp版
    @RequestMapping("/search")
    public ModelAndView search(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,@RequestParam(defaultValue = "8",value = "pageSize") int pageSize, String name, Integer type, Double minPrice, Double maxPrice, Integer up, Integer down, Map<String,Object> map){
        PageHelper.startPage(pageNum,8);
        List<Goods> allGoods = goodsService.searchGoods(map);
        map.put("types",goodsTypeService.getAll());
        map.put("name",name);
        map.put("type",type);
        map.put("minPrice",minPrice);
        map.put("maxPrice",maxPrice);
        map.put("up",up);
        map.put("down",down);
        ModelAndView mav = new ModelAndView("list");
        PageInfo<Goods> pageInfo = new PageInfo<>(allGoods);
        mav.addObject("goodsList",allGoods);
        mav.addObject("pageInfo",pageInfo);
        return  mav;
    }
//    商品详情
    @RequestMapping("/info")
    public ModelAndView info(Integer goodsid){
        ModelAndView mav = new ModelAndView("/goodsinfo");
        Goods goodsById = goodsService.getGoodsById(goodsid);
        if (goodsById!=null){
            mav.addObject("goods",goodsById);
            List<GoodsComment> commentByGoodsId = goodsCommentService.getCommentByGoodsId(goodsById.getId());
            mav.addObject("comment",commentByGoodsId);
        }
        return mav;
    }

//json格式的所有商品搜索
    @RequestMapping(value = "/allGoods",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String allGoods(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,@RequestParam(defaultValue = "8",value = "pageSize") int pageSize, String name, Integer type, Double minPrice, Double maxPrice, Integer up, Integer down, Map<String,Object> map){
        map.put("name",name);
        map.put("type",type);
        map.put("minPrice",minPrice);
        map.put("maxPrice",maxPrice);
        map.put("up",up);
        map.put("down",down);
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> allGoods = goodsService.searchGoods(map);
        JSONObject json = new JSONObject();
        json.put("data",allGoods);
        json.put("code",0);
        json.put("count",goodsService.searchGoods(map).size());
        return  json.toString();
    }

//    直接获取所有goods对象返回json 格式 用来给select 赋值
    @RequestMapping(value = "/jsonGoods",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String jsonGoods(){
        JSONObject json = new JSONObject();
        List<Goods> allGoods = goodsService.getAllGoods();
        json.put("goods",allGoods);
        return json.toString();
    }

    @RequestMapping(value ="/updata" ,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String  updata(Goods goods){
        JSONObject json =  new JSONObject();
        Goods goodsById = goodsService.getGoodsById(goods.getId());
        if(goodsById!=null){
            if(goods.getPrice()>0){
                goodsById.setPrice(goods.getPrice());
                goodsById.setName(goods.getName());
                int i = goodsService.updataGoods(goodsById);
                json.put("state",i);
            }else{
                json.put("state",-2);
            }

        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value ="/setGoodsState" ,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String  setGoodsState(Goods goods){
        JSONObject json =  new JSONObject();
        Goods goodsById = goodsService.getGoodsById(goods.getId());
        if(goodsById!=null){
            int i = goodsService.setGoodsState(goods);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value ="/setGoodsType" ,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String  setGoodsType(Goods goods){
        JSONObject json =  new JSONObject();
        Goods goodsById = goodsService.getGoodsById(goods.getId());
        if(goodsById!=null){
            int i = goodsService.setGoodsType(goods);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
//    添加商品
    @RequestMapping(value ="/addGoods" ,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String  addGoods(Goods goods){
        JSONObject json = new JSONObject();
        if(goods!=null){
            int i = goodsService.addGoods(goods);
            json.put("state",i);
        }else{
//            商品不存在
            json.put("state",-1);
        }
        return json.toString();
    }

    @RequestMapping(value = "/upload",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest req,Integer goodsid){
        JSONObject json = new JSONObject();
            //        获取upload上传路径
            String realPath = req.getServletContext().getRealPath("/img/goods");
//        获取文件名称
            String originalFilename = file.getOriginalFilename();
//       生成随机序列
            String id = UUID.randomUUID().toString();
//        拼接文件名称（防止重名）
            String fileName = id+"_"+originalFilename;
//        拼接文件路径
            String realFileName = realPath+"/"+fileName;
//        获取文件对象
            File f = new File(realFileName);
            try {
//            文件上传
                file.transferTo(f);
                json.put("state",1);
                json.put("filePath","img/goods/"+fileName);
            } catch (IOException e) {
                e.printStackTrace();
//            上传失败
                json.put("state",-1);
            }
            if(goodsid!=null){
                Goods goodsById = goodsService.getGoodsById(goodsid);
                if(goodsById!=null){
                    goodsById.setImg("img/goods/"+fileName);
                    int i = goodsService.setGoodsImg(goodsById);
                    json.put("state",i);
                }else{
                    json.put("state",-2);
                }
            }

        return json.toString();
    }

    @RequestMapping(value = "/deleteAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteAll( @RequestBody List<Goods> goods){
        JSONObject json = new JSONObject();
        if(goods!=null&&goods.size()>0){
            int i = 0;
            for (Goods o:goods) {
                i+=goodsService.deleteGoods(o.getId());
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/SttingState1",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String Stting1( @RequestBody List<Goods> goods){
        JSONObject json = new JSONObject();
        if(goods!=null&&goods.size()>0){
            int i = 0;
            for (Goods o:goods) {
                o.setState(1);
                i+=goodsService.setGoodsState(o);
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/SttingState0",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String Stting0( @RequestBody List<Goods> goods){
        JSONObject json = new JSONObject();
        if(goods!=null&&goods.size()>0){
            int i = 0;
            for (Goods o:goods) {
                o.setState(0);
                i+=goodsService.setGoodsState(o);
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }


}
